package com.xm.xmapp.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xm.xmapp.R;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.response.bean.CustomImageItem;
import com.xm.xmapp.tools.ScreenUtils;

public class ImagePublishAdapter extends BaseAdapter {
	private List<CustomImageItem> mDataList = new ArrayList<CustomImageItem>();
	private Context mContext;
	private RemoveDataListener mRemoveDataListener;

	public ImagePublishAdapter(Context context, List<CustomImageItem> dataList, RemoveDataListener mRemoveDataListener) {
		this.mContext = context;
		this.mDataList = dataList;
		this.mRemoveDataListener = mRemoveDataListener;
	}

	public interface RemoveDataListener {
		void dataChange();
	}

	public int getCount() {
		// 多返回一个用于展示添加图标
		if (mDataList == null && mDataList.size() == 0) {
			return 1;
		} else if (mDataList.size() == CodeConstant.MAX_IMAGE_SIZE) {
			return CodeConstant.MAX_IMAGE_SIZE;
		} else {
			return mDataList.size() + 1;
		}
	}

	public Object getItem(int position) {
		if (mDataList != null && mDataList.size() == CodeConstant.MAX_IMAGE_SIZE) {
			return mDataList.get(position);
		}

		else if (mDataList == null || position - 1 < 0 || position > mDataList.size()) {
			return null;
		} else {
			return mDataList.get(position - 1);
		}
	}

	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("ViewHolder")
	public View getView(int position, View convertView, ViewGroup parent) {
		// 所有Item展示不满一页，就不进行ViewHolder重用了，避免了一个拍照以后添加图片按钮被覆盖的奇怪问题
		convertView = View.inflate(mContext, R.layout.item_publish, null);
		ImageView imageIv = (ImageView) convertView.findViewById(R.id.item_grid_image);
		ImageView imageCha = (ImageView) convertView.findViewById(R.id.item_onclik);

		ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) imageIv.getLayoutParams();
		// 获取当前控件的布局对象
		params.width = ScreenUtils.getScreenWidth(mContext) / 3;
		params.height = params.width;// 设置当前控件布局的高度
		imageIv.setLayoutParams(params);// 将设置好的布局参数应用到控件中

		if (isShowAddItem(position)) {
			imageIv.setImageResource(R.mipmap.feedback_icon_photo);
			imageIv.setBackgroundResource(R.color.bg_gray);
			imageIv.setScaleType(ScaleType.FIT_XY);
			imageCha.setVisibility(View.GONE);
		} else {
			imageIv.setScaleType(ScaleType.CENTER_CROP);
			imageCha.setVisibility(View.VISIBLE);
			final CustomImageItem item = mDataList.get(position);
			ImageLoader.getInstance().displayImage("file://" + item.sourcePath, imageIv);
			imageCha.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					mDataList.remove(item);
					notifyDataSetChanged();
					if (mRemoveDataListener != null) {
						mRemoveDataListener.dataChange();
					}
				}
			});
		}

		return convertView;
	}

	private boolean isShowAddItem(int position) {
		int size = mDataList == null ? 0 : mDataList.size();
		return position == size;
	}

}
